package com.freshfresh.activity.classification;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.freshfresh.activity.BaseActivity;
import com.freshfresh.activity.R;
import com.freshfresh.adapter.GridAdapter;
import com.freshfresh.adapter.GridTypeAdapter;
import com.freshfresh.utils.UrlConstants;
import com.freshfresh.utils.Utils;
import com.freshfresh.view.GridViewWithHeaderAndFooter;
import com.freshfresh.view.refresh.PullToRefreshView;
import com.freshfresh.view.refresh.PullToRefreshView.OnFooterRefreshListener;
import com.freshfresh.view.refresh.PullToRefreshView.OnHeaderRefreshListener;
//二级分类
public class ExclusiveActivity extends BaseActivity implements
OnHeaderRefreshListener, OnFooterRefreshListener{
	List<Map<String,Object>> listmap_data;
	List<Map<String,Object>> listmap_data_bottom;
	Dialog progressDialog;
	GridAdapter adapter;
	GridView gv_type;
	/**
	 * 上拉刷新的控件
	 */
	private GridViewWithHeaderAndFooter gv_activity;
	private TextView tv_title=null;
	private RelativeLayout rel_back=null;
	private PullToRefreshView mPullToRefreshView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_exclusive);
		initView();
		initData();
	}
	public void initData(){
		progressDialog=Utils.initProgressDialog(ExclusiveActivity.this);
		progressDialog.show();
		Map<String, String> params = new HashMap<String, String>();
		if(getIntent().getExtras()!=null){
		params.put("categoryid", getIntent().getExtras().getString("id"));
		Log.e("params是多少…………", getIntent().getExtras().getString("id"));
		}
		
		String ad_url = UrlConstants.product_ejfl_url;
		Log.e("url是多少…………", ad_url);
		executeRequest(new StringRequest(ad_url, params,
				new Response.Listener<String>() {
					public void onResponse(String arg0) {
						progressDialog.dismiss();
						Log.e("listmap的……", arg0);
						if (Utils.parseJsonStr(arg0).get("result").toString()
								.equals("1")) {
							Map<String,Object> map=(Map<String,Object>)Utils.parseJsonStr(arg0).get("data");
							if(!map.containsKey("product_list")){
								showToast(ExclusiveActivity.this, "服务器异常");
								return;
							}
							Map<String,Object> product_list_map=(Map<String,Object>)map.get("product_list");
							if(!product_list_map.containsKey("product_info")){
								showToast(ExclusiveActivity.this, "服务器异常");
								return;
							}
							listmap_data=(List<Map<String,Object>>)product_list_map.get("product_info");
							adapter=new GridAdapter(ExclusiveActivity.this, listmap_data);
							gv_activity.setAdapter(adapter);
							gv_activity.setOnItemClickListener(new OnItemClickListener() {

								@Override
								public void onItemClick(AdapterView<?> arg0,
										View arg1, int arg2, long arg3) {
									String str_product_id="";
									String sku="";
									String str_categories="";
									if(listmap_data.get(arg2).get("product_id")!=null){
										str_product_id=listmap_data.get(arg2).get("product_id").toString();	
									}
									if(listmap_data.get(arg2).get("sku")!=null){
										sku=listmap_data.get(arg2).get("sku").toString();	
									}
									if(listmap_data.get(arg2).get("categories")!=null){
										str_categories=listmap_data.get(arg2).get("categories").toString();	
									}
									Intent intent=new Intent(ExclusiveActivity.this,ProductDetailsActivity.class);
									intent.putExtra("productid", str_product_id);
									intent.putExtra("categoryid", str_categories);
									intent.putExtra("sku", sku);
									startActivity(intent);
								}
								
							});
							
							
							
						} else {
							
						}
					}

				}, new Response.ErrorListener() {

					public void onErrorResponse(VolleyError arg0) {
						// dialog.dismiss();
						Toast.makeText(ExclusiveActivity.this, "获取失败", 1).show();
						progressDialog.dismiss();
					}
				}));
	
	}
	
	
	public void refreshData(){
		progressDialog=Utils.initProgressDialog(ExclusiveActivity.this);
		progressDialog.show();
		Map<String, String> params = new HashMap<String, String>();
		if(getIntent().getExtras()!=null){
		params.put("categoryid", getIntent().getExtras().getString("id"));
		  }
		String ad_url = UrlConstants.product_ejfl_url;
		Log.e("url是多少…………", ad_url);
		executeRequest(new StringRequest(ad_url, params,
				new Response.Listener<String>() {
					public void onResponse(String arg0) {
						progressDialog.dismiss();
						Log.e("listmap的……", arg0);
						if (Utils.parseJsonStr(arg0).get("result").toString()
								.equals("1")) {
							Map<String,Object> map=(Map<String,Object>)Utils.parseJsonStr(arg0).get("data");
							Map<String,Object> product_list_map=(Map<String,Object>)map.get("product_list");
							List<Map<String,Object>> listmap_data_refresh=(List<Map<String,Object>>)product_list_map.get("product_info");
							adapter.notifyData(listmap_data_refresh);
							
							mPullToRefreshView.onHeaderRefreshComplete("更新于:"+new Date().toLocaleString());
							mPullToRefreshView.onHeaderRefreshComplete();
							
							
						} else {
							
						}
					}

				}, new Response.ErrorListener() {

					public void onErrorResponse(VolleyError arg0) {
						// dialog.dismiss();
						Toast.makeText(ExclusiveActivity.this, "获取失败", 1).show();
						progressDialog.dismiss();
					}
				}));
	
	}
	
	public void loadMoreData(){
		progressDialog=Utils.initProgressDialog(ExclusiveActivity.this);
		progressDialog.show();
		Map<String, String> params = new HashMap<String, String>();
		if(getIntent().getExtras()!=null){
			params.put("categoryid", getIntent().getExtras().getString("id"));
			  }
		String ad_url = UrlConstants.product_ejfl_url;
		Log.e("url是多少…………", ad_url);
		executeRequest(new StringRequest(ad_url, params,
				new Response.Listener<String>() {
					public void onResponse(String arg0) {
						progressDialog.dismiss();
						Log.e("listmap的……", arg0);
						if (Utils.parseJsonStr(arg0).get("result").toString()
								.equals("1")) {
							Map<String,Object> map=(Map<String,Object>)Utils.parseJsonStr(arg0).get("data");
							Map<String,Object> product_list_map=(Map<String,Object>)map.get("product_list");
							List<Map<String,Object>> listmap_data_refresh=(List<Map<String,Object>>)product_list_map.get("product_info");
							adapter.notifyData(listmap_data_refresh);
							
							mPullToRefreshView.onFooterRefreshComplete();
							
							
						} else {
							
						}
					}

				}, new Response.ErrorListener() {

					public void onErrorResponse(VolleyError arg0) {
						// dialog.dismiss();
						Toast.makeText(ExclusiveActivity.this, "获取失败", 1).show();
						progressDialog.dismiss();
					}
				}));
	
	}
	

	public void initView() {
		// 得到控件
		
		gv_activity = (GridViewWithHeaderAndFooter) findViewById(R.id.gv_activity);
		View vv=getLayoutInflater().inflate(R.layout.ac_classification_bottom, null);
		gv_type=(GridView)vv.findViewById(R.id.gv_type);
		rel_back=(RelativeLayout)findViewById(R.id.rel_back);
		gv_activity.addFooterView(vv);
		initBottomData();
		
		mPullToRefreshView = (PullToRefreshView) findViewById(R.id.main_pull_refresh_view);
		mPullToRefreshView.setOnHeaderRefreshListener(this);
		mPullToRefreshView.setOnFooterRefreshListener(this);
		mPullToRefreshView.setLastUpdated(new Date().toLocaleString());
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_title.setText("二级分类");
		rel_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				ExclusiveActivity.this.finish();
			}
		});
		
		
	}
	//底部分类数据
	public void initBottomData(){
//		progressDialog=Utils.initProgressDialog(ExclusiveActivity.this);
//		progressDialog.show();
		Map<String, String> params = new HashMap<String, String>();
		String ad_url = UrlConstants.product_ejfl_bottom;
		Log.e("url是多少…………", ad_url);
		executeRequest(new StringRequest(ad_url, params,
				new Response.Listener<String>() {
					public void onResponse(String arg0) {
//						progressDialog.dismiss();
						Log.e("listmap的……", arg0);
						if (Utils.parseJsonStr(arg0).get("result").toString()
								.equals("1")) {
							Map<String,Object> map=(Map<String,Object>)Utils.parseJsonStr(arg0).get("data");
							listmap_data_bottom=(List<Map<String,Object>>)map.get("category_list");
							GridTypeAdapter adaptertype=new GridTypeAdapter(ExclusiveActivity.this, listmap_data_bottom);
							gv_type.setAdapter(adaptertype);
							
							
							
							
						} else {
							
						}
					}

				}, new Response.ErrorListener() {

					public void onErrorResponse(VolleyError arg0) {
						// dialog.dismiss();
						Toast.makeText(ExclusiveActivity.this, "获取失败", 1).show();
						progressDialog.dismiss();
					}
				}));
	
	}
	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		// TODO Auto-generated method stub
		loadMoreData();
	}
	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
		// TODO Auto-generated method stub
		refreshData();
	}
	
}
